﻿
Public Class LoginFrm
    Private Sub LimpiarCajasDeTexto()
        For Each micaja As Control In Me.Controls
            If micaja.GetType = GetType(TextBox) Then TryCast(micaja, TextBox).Clear()
        Next
        UsuarioTextBox.Select()
    End Sub

    Private Sub BtnEntrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEntrar.Click
        For Each micontrol As Control In Me.Controls
            If micontrol.GetType = GetType(TextBox) Then
                If micontrol.Text = "" Then
                    MessageBox.Show("Las cajas de texto no pueden estar vacías")
                    Exit Sub
                End If
                
            End If
        Next
        Dim drUsuarios() As scrumDataSet.UsuariosRow = ScrumDataSet.Tables("Usuarios").Select("Usuario = '" & UsuarioTextBox.Text & "' And Contraseña = '" & ContraseñaTextBox.Text & "'")
        If drUsuarios.Length <= 0 Then
            MessageBox.Show("ERROR de login, porfavor introduzca datos correctos")
            UsuarioTextBox.Select()
            LimpiarCajasDeTexto()
            Exit Sub
        End If

        If drUsuarios(0).Nivel = 0 Then
            Dim boss As New TheBossFrm
            Me.Close()
            boss.ShowDialog()
        ElseIf drUsuarios(0).Nivel = 1 Then
            Dim empl As New frmEmpleado
            Me.Close()
            empl.ShowDialog()
        ElseIf drUsuarios(0).Nivel = 2 Then
            Dim cli As New frmCliente
            Me.Close()
            cli.ShowDialog()
        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalir.Click
        NuevoUsuarioFrm.UsuariosTableAdapter.Fill(NuevoUsuarioFrm.ScrumDataSet.Usuarios)
        Me.Close()

    End Sub

    Private Sub BtnRegistrarte_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRegistrarte.Click

        Dim Child As NuevoUsuarioFrm
        Child = New NuevoUsuarioFrm
        LimpiarCajasDeTexto()
        Child.ShowDialog()
        Login_Load(sender, e)  ' Cargar los nuevos registros
    End Sub

    Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.UsuariosTableAdapter.Fill(ScrumDataSet.Usuarios)

        UsuarioTextBox.Select()
    End Sub
End Class